
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Topics;

public class GenericTopicsDAO extends AutoKeys implements org.jetnuke.dao.TopicsDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#retrieveByPK(int)
	 */
	public Topics retrieveByPK(int topicId) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel2.selectByID"));
		p.setInt(1, topicId);
		
		ResultSet rs = p.executeQuery();
		Topics t = new Topics();
		
		if (rs.next()) {
			fillTopicsFromResultSet(t, rs);
		}
		
		rs.close();
		p.close();

		return t;
	}

	/**
	 * @param t
	 * @param rs
	 */
	protected void fillTopicsFromResultSet(Topics t, ResultSet rs)throws Exception {
		t.setCOUNTER(rs.getInt("COUNTER"));
		t.setTOPICID(rs.getInt("TOPICID"));
		t.setTOPICIMAGE(rs.getString("TOPICIMAGE"));
		t.setTOPICNAME(rs.getString("TOPICIMAGE"));
		t.setTOPICTEXT(rs.getString("TOPICTEXT"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#addNew(net.jforum.entities.Topic)
	 */
	public int addNew(Topics top) throws Exception{
		PreparedStatement p = this.getStatementForAutoKeys("TopicModel2.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("TopicModel2.lastGeneratedTopicId"));
		initNewTopic(top, p);
		
		int id = this.executeAutoKeysQuery(p);
				
		return id;
		
	}

	/**
	 * @param top
	 * @param p
	 */
	private void initNewTopic(Topics top, PreparedStatement p) throws Exception{
		p.setString(1,top.getTOPICNAME());
		p.setString(2,top.getTOPICIMAGE());
		p.setString(3,top.getTOPICTEXT());
		p.setInt(4,0);
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#update(org.jetnuke.entities.Topics)
	 */
	public void update(Topics top) throws Exception {
		PreparedStatement p =JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel2.update"));
		p.setString(1,top.getTOPICNAME());
		p.setString(2,top.getTOPICIMAGE());
		p.setString(3,top.getTOPICTEXT());
		p.setInt(4,top.getCOUNTER());
		p.setInt(5,top.getTOPICID());
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#selectAllOrderTopicName()
	 */
	public List selectAllOrderTopicName() throws Exception {
		PreparedStatement p;
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel2.selectAllOrderTopicName"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Topics u = new Topics();
			fillTopicsFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p;
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel2.selectAll"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.TopicsModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel2.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

}
